#include "LoginUI.h"
#include "ui_login.h"

#include "ForgotPasswordUI.h"
#include "Authentication.h"

#include "UIManager.h"

#include <QMessageBox>

Login::Login(UIManager *manager, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Login)
{
    ui->setupUi(this);
    this->manager = manager;

    ui->labelErrorInfo->setStyleSheet("QLabel{color : #ff0000}");

    setFixedSize(size());
    show();
}

Login::~Login()
{
    delete ui;
}

void Login::on_buttonLogin_clicked()
{
    Authentication *auth = Authentication::getInstance();

    QString username = ui->fieldUsername->text();
    QString password = ui->fieldPassword->text();

    bool loginSuccess = auth->login(username, password);

    if (loginSuccess)
    {
        hide();
        manager->showDashboard();
    }
    else
    {
        ui->labelErrorInfo->setText("Invalid username or password");
    }

}

void Login::on_labelForgotPassword_linkActivated(const QString)
{
    ForgotPasswordUI *forgotPassword = new ForgotPasswordUI();

    forgotPassword->setFixedSize(forgotPassword->size());
    forgotPassword->show();
}
